package sparkSql

import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.{SparkConf, SparkContext, sql}
import org.junit.{After, Before, Test}

class ReadJson {
/*	val conf: SparkConf = new SparkConf().setAppName("sparkSql").setMaster("local[3]")
  val spark: SparkSession = SparkSession.builder().config(conf).getOrCreate()*/

  private val spark: SparkSession = SparkSession.builder().appName("sparkSql").master("local[3]").getOrCreate()

	var outPath: String = "out"


	import util.MyPredef._

	@Before
	def init() {
		outPath.delete()
	}

	@After
	def after() {
		spark.stop()
	}

	/**
	 * 1、构建sparkSession 对象
	 * 2、读取数据
	 * 3、输出或展示
	 */
	@Test
	def queryJson(): Unit = {

		val df: DataFrame = spark.read.json("in/user.json")
		df.show()
	}

  /**
   * sql 测试
   */
	@Test
	def sql(): Unit = {
		val df: DataFrame = spark.read.json("in/user.json")
		df.createOrReplaceTempView("user")

		val user: DataFrame = spark.sql("select '常量', * from user")
		user.show()
	}
}
